package com.lagou.homework4.no1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件读取类
 */
public class ReaderStudentFile {
    public static List readerFile(){
        ObjectInputStream ois = null;
        List<Student> list = new ArrayList();//数据集合
        File file = new File("d:/test.txt");//读取文件
        if(file.exists()){
            try {
                // 1.创建ObjectInputStream类型的对象与d:/test.txt文件关联
                ois = new ObjectInputStream(new FileInputStream("d:/test.txt"));
                // 2.从输入流中读取一个对象并打印
                list = (List<Student>) ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                // 3.关闭流对象并释放有关的资源
                if (null != ois) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list;
    }
}
